import {
  login,
  getUserInfo
} from '../api/common.api'
import $vue from '@/main'
import uitl, {default as util} from '@/services/util'
export default {
  namespaced: true,
  state: {
    rtk: null,
    un: null,
    uid: null
  },
  mutations: {

  },
  actions: {
    initUser ({state}) {
      return new Promise((resolve, reject) => {
        const user = util.getItem('$user')
        if (user) {
          state.rtk = user.rtk
          state.uid = user.uid
          state.un = user.un
          resolve(1)
        } else {
          $vue.$router.push({path: '/login'})
          reject(0)
        }
      })
    },
    login ({state}, loginForm) {
      login(loginForm).then(res => {
        if (res.status === 200) {
          const data = res.data[0]
          uitl.setItem('$user', {
            rtk: data.rtk,
            un: data.un,
            uid: data.uid
          })
          state.uid = data.uid
          state.un = data.un
          state.rtk = data.rtk
          $vue.$router.push({path: '/system'})
        } else {
          $vue.$notify.error({
            title: '提示',
            message: res.msg
          })
        }
      })
    },

  }
}
